/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package au.csiro.nt.psdp.actions.user;

/**
 *
 * @author Administrator
 */
import au.csiro.nt.psdp.actions.PdspActionSupport;
import au.csiro.nt.psdp.manager.RoleManager;
import au.csiro.nt.psdp.manager.UserManager;
import au.csiro.nt.psdp.model.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.appfuse.service.MailEngine;
import org.springframework.mail.SimpleMailMessage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copy from Appfuse
 */
public class BaseAction extends PdspActionSupport
{
    private static final long serialVersionUID = 3525445612504421307L;
    protected transient final Log log = LogFactory.getLog(getClass());
    protected UserManager userManager = null;
    protected RoleManager roleManager = null;
    protected String from = null;
    protected String cancel = null;
    protected String delete = null;
    protected String save = null;
    protected MailEngine mailEngine = null;
    protected SimpleMailMessage mailMessage = null;
    protected String templateName = null;

    public String cancel()
    {
        return CANCEL;
    }

    @SuppressWarnings("unchecked")
    protected void saveMessage(String msg)
    {
        List messages = (List) getSession().getAttribute("messages");
        if (messages == null)
        {
            messages = new ArrayList();
        }
        messages.add(msg);
        getSession().setAttribute("messages", messages);
    }

    protected void sendUserMessage(User user, String msg, String url)
    {
        if (log.isDebugEnabled())
        {
            log.debug("sending e-mail to user [" + user.getEmail() + "]...");
        }

        mailMessage.setTo(user.getUsername() + "<" + user.getEmail() + ">");

        Map<String, Object> model = new HashMap<String, Object>();
        model.put("user", user);
        // TODO: figure out how to get bundle specified in webwork.properties
        // model.put("bundle", getTexts());
        model.put("message", msg);
        model.put("applicationURL", url);
        mailEngine.sendMessage(mailMessage, templateName, model);
    }

    public void setUserManager(UserManager userManager)
    {
        this.userManager = userManager;
    }

    public void setRoleManager(RoleManager roleManager)
    {
        this.roleManager = roleManager;
    }

    public void setMailEngine(MailEngine mailEngine)
    {
        this.mailEngine = mailEngine;
    }

    public void setMailMessage(SimpleMailMessage mailMessage)
    {
        this.mailMessage = mailMessage;
    }

    public void setTemplateName(String templateName)
    {
        this.templateName = templateName;
    }

    /**
     * Convenience method for setting a "from" parameter to indicate the previous page.
     *
     * @param from indicator for the originating page
     */
    public void setFrom(String from)
    {
        this.from = from;
    }

    public void setDelete(String delete)
    {
        this.delete = delete;
    }

    public void setSave(String save)
    {
        this.save = save;
    }
}
